package io.adrainty.bolee.insurance.repository.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.adrainty.bolee.insurance.models.bo.ConditionBO;
import io.adrainty.bolee.insurance.mapper.ConditionMapper;
import io.adrainty.bolee.insurance.models.entity.ConditionEntity;
import io.adrainty.bolee.insurance.repository.IConditionRepository;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.List;

/**
 * <p>ConditionServiceRepositoryImpl</p>
 *
 * @author AdRainty
 * @version V1.0.0
 * @description 保障项系数管理
 * @since 2025/8/11 10:23:35
 */

@Repository
public class ConditionRepositoryImpl implements IConditionRepository {

    @Resource
    private ConditionMapper conditionMapper;

    @Override
    public IPage<ConditionEntity> findConditionPage(Integer pageNum, Integer pageSize, ConditionEntity searchDTO) {
        Page<ConditionBO> page = new Page<>(pageNum, pageSize);
        Wrapper<ConditionBO> wrapper = getWrapper(searchDTO);
        Page<ConditionBO> selectPage = conditionMapper.selectPage(page, wrapper);
        return selectPage.convert(item -> BeanUtil.toBean(item, ConditionEntity.class));
    }

    @Override
    public List<ConditionEntity> findConditionList(ConditionEntity entity) {
        Wrapper<ConditionBO> wrapper = getWrapper(entity);
        List<ConditionBO> conditionBOList = conditionMapper.selectList(wrapper);
        return BeanUtil.copyToList(conditionBOList, ConditionEntity.class);
    }

    @Override
    public boolean createCondition(ConditionEntity entity) {
        ConditionBO conditionBO = BeanUtil.toBean(entity, ConditionBO.class);
        boolean result = conditionMapper.insert(conditionBO) > 0;
        if (!result) return false;
        BeanUtil.copyProperties(conditionBO, entity);
        return true;
    }

    @Override
    public boolean updateCondition(ConditionEntity entity) {
        ConditionBO conditionBO = BeanUtil.toBean(entity, ConditionBO.class);
        return conditionMapper.updateById(conditionBO) > 0;
    }

    @Override
    public boolean deleteCondition(List<String> deleteIdList) {
        return conditionMapper.deleteByIds(deleteIdList) > 0;
    }

    private static Wrapper<ConditionBO> getWrapper(ConditionEntity searchDTO) {
        return Wrappers.lambdaQuery(ConditionBO.class)
                .eq(StringUtils.isNotBlank(searchDTO.getConditionKey()), ConditionBO::getConditionKey, searchDTO.getConditionKey())
                .eq(StringUtils.isNotBlank(searchDTO.getConditionKeyName()), ConditionBO::getConditionKeyName, searchDTO.getConditionKeyName())
                .in(ArrayUtil.isNotEmpty(searchDTO.getConditionKeys()), ConditionBO::getConditionKey, Arrays.stream(searchDTO.getConditionKeys()).toList())
                .eq(searchDTO.getDataState() != null, ConditionBO::getDataState, searchDTO.getDataState())
                .orderByAsc(ConditionBO::getSortNo)
                .orderByDesc(ConditionBO::getCreatedTime);
    }

}
